<template>
    <div>

    </div>
</template>

<script setup lang="ts">
import router from '@/router';
import { LoginParams } from '@/api/auth/types'
import { useUserStore } from '@/store/modules/user';
import { LocationQuery, LocationQueryValue, useRoute } from 'vue-router';

const route = useRoute()

const userStore = useUserStore()

const LogParams = reactive<LoginParams>({
    username: '',
    password: ''
});

function handleLogin() {
    userStore
        .login({
            username: LogParams.username,
            password: LogParams.password,
        })
        .then(() => {
            const query: LocationQuery = route.query;
            const redirect = (query.redirect as LocationQueryValue) ?? '/';
            const otherQueryParams = Object.keys(query).reduce(
                (acc: any, cur: string) => {
                    if (cur !== 'redirect') {
                        acc[cur] = query[cur];
                    }
                    return acc;
                },
                {}
            );

            router.push({ path: redirect });
        })
}

onBeforeMount(() => {
    const { username, password } = route.query
    if (username && password) {
        LogParams.username = username as any
        LogParams.password = password as any
        handleLogin()
    }
})
</script>

<style scoped></style>